package com.kedacom.ctsp.authority.service.simple.strategy;


import com.kedacom.ctsp.authority.entity.ResourceSettingRelation;
import com.kedacom.ctsp.authority.service.ResourceSettingRelationService;
import com.kedacom.ctsp.orm.param.Param;
import com.kedacom.ctsp.orm.param.TermEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Set;

import static com.kedacom.ctsp.authority.entity.ResourceSettingRelation.RESOURCE_ID;
import static com.kedacom.ctsp.web.entity.StatefulBean.STATUS;

@Slf4j
public abstract class ResourceChangeStrategy {

    @Autowired
    private ResourceSettingRelationService resourceSettingRelationService;

    protected abstract Integer getQueryStatus();

    protected abstract Integer getUpdateStatus();


    public void onChangeEvent(Set<String> ids) {

        log.info("onChangeEvent: {} ", this.getClass().getSimpleName());

        if (CollectionUtils.isEmpty(ids)) {
            log.warn(" ids is empty !!");
            return;
        }

        Param param = new Param();
        param.and(RESOURCE_ID, TermEnum.in, ids);
        param.and(STATUS, getQueryStatus());

        List<ResourceSettingRelation> updateList = resourceSettingRelationService.select(param);
        if (CollectionUtils.isNotEmpty(updateList)) {
            updateList.stream().forEach(one -> one.setStatus(getUpdateStatus()));
            log.info("更新 resourceSettingRelation size: {} ", updateList.size());
            resourceSettingRelationService.update(updateList);
        }

    }
}
